package Chapter9;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Example20 extends JFrame{
	private ButtonGroup group;//单选按钮对象
	private JPanel panel;//JPanel面板放置三个JRaioButton按钮
	private JPanel pallet;//JPanel面板作为调色板
	public Example20() {
		pallet=new JPanel();
		this.add(pallet, BorderLayout.CENTER);
		panel=new JPanel();
		group=new ButtonGroup();
		addJRadioButton("灰");
		addJRadioButton("黄");
		addJRadioButton("粉");
		this.add(panel, BorderLayout.SOUTH);
		this.setSize(300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	private void addJRadioButton(final String text) {
		JRadioButton radioButton=new JRadioButton(text);
		group.add(radioButton);
		panel.add(radioButton);
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color color=null;
				if("灰".equals(text)) {
					color=Color.gray;
				}else if("黄".equals(text)) {
					color=Color.YELLOW;
				}else if("粉".equals(text)) {
					color=Color.PINK;
				}else {
					color=Color.WHITE;
				}
				pallet.setBackground(color);
			}
		});
	}
	public static void main(String[] args) {
		new Example20();
	}
}
